const {ccclass, property} = cc._decorator;
const bundleUrl = 'http://192.168.52.44:3434';

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    lessonContainer: cc.Node = null;


    onClickLoadBundle() {
        this._loadDefaultBundle(() => {
            // @ts-ignore
            const {eduGame} = window.__require('EduGame')
            // @ts-ignore
            const {GameConfig, BundleLoader} = window.__require('GameConfig');
            BundleLoader.url = bundleUrl;
            eduGame.addEventListener(eduGame.Event.OnInitialize, () => {
                const lessonID = '8c620662-2fef-4390-b444-ff48a743364a'; // 要加载胡课程
                const lessonInfo = GameConfig.data.find(el => el.id === lessonID);
                if (lessonInfo && lessonInfo.pages.length > 0) {
                    eduGame.loadPage(lessonInfo.pages[0])
                } else {
                    cc.error(`未发现课程信息：${lessonID}`)
                }
            })
            let eduRootNode = eduGame.init();
            this.node.addChild(eduRootNode);
        })
    }

    _getBundleName(name) {
        return `${name}-ice`
    }

    _loadDefaultBundle(cb) {
        cc.assetManager.loadBundle(`${bundleUrl}/${this._getBundleName('internal')}`, (error, bundle) => {
            if (error) {
                return
            }
            cc.log('load internal')
            cc.assetManager.loadBundle(`${bundleUrl}/${this._getBundleName('resources')}`, (err, bundle1) => {
                if (err) {
                    return
                }
                cc.log('load resources')
                cc.assetManager.loadBundle(`${bundleUrl}/${this._getBundleName('main')}`, (err1, bundle2) => {
                    if (err1) {
                        return
                    }
                    cc.log('load main')
                    cb && cb()
                })
            })

        });
    }
}
